##############################################################################
# Filename: Analysis_ArticleEvaluation_written.R
# Purpose: Produce Table 2 in main text and information in SI Section A3
##############################################################################

source("Setup.R")

### Table A2: Types of written feedback
table(data_full$CommentBiased)
table(data_full$CommentFeeling)

### For main text analyses, set neutral values for those in treatment groups who didn't write a comment
data_full$CommentBiased[is.na(data_full$CommentBiased)&data_full$Article%in%c("DN","DP")] <- 0
data_full$CommentFeeling[is.na(data_full$CommentFeeling)&data_full$Article%in%c("DN","DP")] <- 2

### Separate out those who were in treatment groups 
data_eval = data_full[which(data_full$CompletionStatus=="Finished" | data_full$CompletionStatus=="PolicyopsDone" | data_full$CompletionStatus=="NewsConsArea"),]
data_eval = data_eval[which(data_eval$Article!="pl"),]
data_eval = droplevels(data_eval)

### Separate out those who were in treatment groups & wrote a (related) comment 
data_wrote = data_eval[data_eval$wrote==1,]
data_wrote = data_wrote %>% drop_na(Coder1_Feeling, Coder2_Feeling, Coder3_Feeling)

### In-text information at the beginning of Section A3
# Number of comment writers in treatment groups
table(data_eval$wrote)
# Average number of words 
mean(data_eval$wordcount[data_eval$wrote==1])
# Number of unclear/unrelated comments 
table(data_eval$wrote.unclear)

### Intercoder agreement
# Mentioned Bias
data_labels_B = data_wrote[,c("Coder1_Biased","Coder2_Biased","Coder3_Biased")]
kappam.fleiss(data_labels_B)
agree(data_labels_B[,2:3])
agree(data_labels_B[,1:2])
agree(data_labels_B[,c(1,3)])
# Article Feelings
data_labels_F = data_wrote[,c("Coder1_Feeling","Coder2_Feeling","Coder3_Feeling")]
kappa2(data_labels_F[,c(1,3)], "equal")
agree(data_labels_F[,2:3])
agree(data_labels_F[,1:2])
agree(data_labels_F[,c(1,3)])
# Check how many comments had three different labels (answer: none)
label.congruence = ifelse(data_wrote$Coder1_Feeling==data_wrote$Coder2_Feeling, 1,
           ifelse(data_wrote$Coder1_Feeling==data_wrote$Coder3_Feeling, 1,
           ifelse(data_wrote$Coder2_Feeling==data_wrote$Coder3_Feeling, 1, 0)))
table(label.congruence)
rm(data_labels_B, data_labels_F, label.congruence)

### "Predicting" commenting (footnote 7, Section A3) 
summary(lm(wrote ~ CollegeGrad + as.numeric(Income) + Age + Male + Hindu + Caste,
           data = data_full))
data_eval %>% group_by(Caste) %>% summarise(mean(wrote))
data_eval %>% group_by(Article_Alignment) %>% summarise(mean(wrote))

### Table 2: Effect of article read and level of BJP support on 
          #  written article feedback
          #  (Sample: Full treatment sample)
mod_bias_everyone_main = lm(CommentBiased ~ ProBJP*Article + CollegeGrad + NewsDaily + StrongInterestPolitics, data = data_eval) 
mod_feel_everyone_main  = lm(as.numeric(CommentFeeling) ~ ProBJP*Article + CollegeGrad + NewsDaily + StrongInterestPolitics, data = data_eval)
stargazer(mod_bias_everyone_main, mod_feel_everyone_main,
          align=TRUE, keep.stat="n", no.space=TRUE,
          se = starprep(mod_bias_everyone_main),
          covariate.labels=c("Pro-BJP","Positive Article",
                             "College Graduate", "Daily News", "Interested in Politics",
                             "Pro-BJP x Positive Article"))
rm(mod_bias_everyone_main, mod_feel_everyone_main)

### Table A3: Effect of article read and level of BJP support on 
           #  written article feedback
           #  (Sample: Comment writers only)
mod_bias_writers_main = lm(CommentBiased ~ ProBJP*Article + CollegeGrad + NewsDaily + StrongInterestPolitics, data = data_wrote)
mod_feel_writers_main  = lm(as.numeric(CommentFeeling) ~ ProBJP*Article + CollegeGrad + NewsDaily + StrongInterestPolitics, data = data_wrote)
stargazer(mod_bias_writers_main, mod_feel_writers_main,
          align=TRUE, keep.stat="n", no.space=TRUE,
          se = starprep(mod_bias_writers_main),
          covariate.labels=c("Pro-BJP","Positive Article",
                             "College Graduate", "Daily News", "Interested in Politics",
                             "Pro-BJP x Positive Article")
          )
rm(mod_bias_writers_main, mod_feel_writers_main)